<?php
class TestingController extends Controller {
	public function actionExam(){
		$this->checkToken();

        Yii::app()->user->setState('exam_session', Yii::app()->request->getRequestUri());

		$params = array();
		$course = Course::model()->findByPk($_REQUEST['course_id']);

        $now = time();
        $time = $_REQUEST['time'] + ($course->exam_time * 60);

        $exam_upload = new ExamUpload();

        if (isset($_POST['exam'])){
            $exam_upload->attributes = $_POST['ExamUpload'];

            $data = $_POST['exam'];
			$exam = new Exam();
			$exam->start = $_REQUEST['time'];
			$exam->exam_time = time();
			$exam->user_id = Yii::app()->user->data()->id;
			$total = 0;
			$total_question = 0;
			if ($course) {
				$exam->exam_name = $course->name;
				$exam->course_id = CourseTmp::model()->getCourseId($course);
			}
			/**/
			if ($exam->save()){
				foreach ($data as $key_id => $item){
					$detail = new ExamDetail();
					
					$ques = Question::model()->findByPk($key_id);
					$detail->exam_id = $exam->id;
					$detail->question_params = json_encode(array('id' => $ques->id, 'title' => $ques->title));
					
					$ans = $item['answer'];
					if ($ques->type == Question::ISingle){
						$right = isset($ans['check']) ? $ans['check'] : 0;
						$arrAns = array();
						foreach ($ans['id'] as $ans_id){
							$tanswer = Answer::model()->findByPk($ans_id);
							$tmp = array('title' => $tanswer->title, 'right' => $tanswer->is_right, 'checked' => false);
							if ($right == $ans_id){
								$tmp['checked'] = true;
								if ($tanswer->is_right){
									$total++;
								}
							}
							
							$arrAns[] = $tmp;
						}
						$detail->answer_params = json_encode(array(Question::ISingle => $arrAns));
					}elseif ($ques->type == Question::IText){
						$detail->answer_params = json_encode(array(Question::IText => $ans['text']));
					}
					
					if ($ques->is_mark) $total_question++;
					$detail->save();
					
				}
				
				
				$exam->total_right = $total;
				$exam->total_question = $total_question;
				$exam->total_mark = $exam->getMark();

                $user_infor = json_decode(Yii::app()->user->getState('user_infor'), true);
                $exam->full_name = $user_infor['full_name'];
                $exam->depart_name = $user_infor['depart_name'];

                $file = CUploadedFile::getInstance($exam_upload,'file');
                if ($file){
                    $info = pathinfo($file->getName());
                    $path = 'upload/' . $exam->id . '_' . $exam->user_id . '_' . Yii::app()->user->data()->username . '_' . str_replace(".", "", microtime(true)) . '.' . $info['extension'];
                    $file->saveAs($path);
                    $exam->file_exam = '/' . $path;
                }

				$exam->save();
			}
            Yii::app()->user->setState('exam_session', '');
			$this->redirect_to(array('result', 'id' => $exam->id));
		}else{
            if ($now >= $time){
                //$this->redirect(Yii::app()->createUrl('/'));
            }
        }

        $time_remain = 0;
        $time_string = "";
        $h = 0; $i = 0; $s = 0;
        if ($now < $time){
            $time_remain = $time - $now;
            if ($time_remain < 60)
                $s = $time_remain;
            else{
                if ($time_remain < 60 * 60){
                    $s = $time_remain % 60;
                    $i = ($time_remain - $s) / 60;
                }else{
                    $h = ($time_remain - ( $time_remain - 3600)) / 3600;
                    $i = $time_remain - 3600;
                    $s = $i % 60;
                    $i = ($i - $s) / 60;
                }
            }
        }

        $params['time_str'] = str_pad($h, 2, "0", STR_PAD_LEFT) .":" . str_pad($i, 2, "0", STR_PAD_LEFT) . ":" . str_pad($s, 2, "0", STR_PAD_LEFT);
        $params['time_remain'] = $time_remain;

		if ($course)
			$params['question'] = Question::model()->getExam($course->total_question);
		else
			$params['question'] = Question::model()->getExam();
		$params['exam_start'] = $_REQUEST['time'];
        $params['exam_upload'] = $exam_upload;
		
		$this->render('exam', $params);
	}
	
	public function actionStart(){
		$params = array();

        $exam_session = Yii::app()->user->getState('exam_session');
        if ($exam_session && !empty($exam_session)){
            $this->redirect($exam_session);
        }

		$course = Course::model()->getExam();
        $check = Exam::model()->checkExam(Yii::app()->user->data()->id, $course->id);

        $model = new InforForm();

        $depart = array(
            'Đoàn Cty CPCN Bến Thành', 'Đoàn Cty CPCN Gia Định', 'Đoàn Cty CPCN Nhà Bè', 'Đoàn Cty CPCN Chợ Lớn', 'Đoàn Cty CPCN Phú Hòa Tân', 'Đoàn Cty CPCN Thủ Đức',
            'Đoàn Cty TNHH MTV CN Trung An', 'Đoàn Cty TNHH MTV CN Tân Hòa', 'Đoàn Khối Phòng Ban Tcty', 'Đoàn Cty TNHH MTV Công trình GTCC', 'Chi đoàn Cty TNHH MTV Tư vấn GTCC',
            'Chi đoàn Cty TNHH MTV Nước ngầm Sài Gòn', 'Chi đoàn Cty CP Tư vấn XDCN', 'Chi đoàn Cty CP Cơ khí Công Trình CN', 'Chi đoàn Xí nghiệp Truyền dẫn Nước sạch',
            'Chi đoàn Cty CP Đầu tư và Kinh doanh Nước Sạch', 'Đoàn Nhà máy nước Thủ Đức', 'Đoàn Nhà máy nước Tân Hiệp'
        );

        if (isset($_POST['InforForm']) && $course && $check){
            $model->attributes = $_POST['InforForm'];

            Yii::app()->user->setState('user_infor', json_encode(array('full_name' => $model->full_name, 'depart_name' => $depart[$model->depart_id])));

			$time = time();
			
			$this->redirect_to(array('exam', 'course_id' => $course->id, 'time' => $time));
		}
		
		$params['course'] = $course;
        $params['depart'] = $depart;
		$params['model'] = $model;
        $params['check'] = $check;
		
		$this->render('exam_start', $params);
	}
	
	public function actionResult(){
		$this->checkToken();
		$params = array();
		$exam = Exam::model()->findByPk($_REQUEST['id']);
		if ($exam){
			$params['exam'] = $exam;
			$params['detail'] = ExamDetail::model()->findAllByAttributes(array('exam_id' => $exam->id));
		}
		
		$this->render('result', $params);
	}
}

?>